package com.yiquan.video.controller;

import java.util.*;

import com.yiquan.video.client.CommentClient;
import com.yiquan.video.pojo.VideoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.yiquan.video.pojo.Video;
import com.yiquan.video.service.VideoService;

import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.web.multipart.MultipartFile;

/**
 * 控制器层
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/video")
@Slf4j
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private CommentClient commentClient;

    /**
     * 根据video_id查询
     *
     * @param videoId ID
     * @return
     */
    @RequestMapping(value = "/getVideoByVideoId", method = RequestMethod.GET)
    public Result findByVideoId(@RequestParam Long videoId) {
        log.info("findByVideoId入参：{}", videoId);
        return new Result(true, StatusCode.OK, "查询成功", videoService.findByVideoId(videoId));
    }


    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", videoService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable Long id) {
        return new Result(true, StatusCode.OK, "查询成功", videoService.findById(id));
    }

    /**
     * 分页+多条件查询
     * 入参支持userId videoId startCreateDate
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size,@RequestParam(required = false) Long operateUserId,@RequestParam(required = false) Integer type) {

        Page<Video> pageList = videoService.findSearch(searchMap, page, size,operateUserId);

//        if (type!=null){
//             pageList.getContent().forEach(video -> {
//                 Map map = new HashMap();
//                 map.put("parent_id",video.getVideo_id());
//                 Result searchLogout = commentClient.findSearchLogout(map);
//                 LinkedHashMap data = (LinkedHashMap) searchLogout.getData();
//                 ArrayList list = (ArrayList) data.get("rows");
//                 video.setVideoComment(list.get(0));
//             });
//        }
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<Video>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return0
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap) {
        return new Result(true, StatusCode.OK, "查询成功", videoService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param video
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Video video) {
        videoService.add(video);
        return new Result(true, StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param video
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public Result update(@RequestBody Video video, @PathVariable Long id) {
        if (video != null) {
            videoService.update(video, id);
        }
        return new Result(true, StatusCode.OK, "更新成功");

    }

    /**
     * 修改
     *
     * @param video
     */
    @PutMapping("/updateByVideoId")
    public Result updateByVideoId(@RequestBody Video video, @RequestParam Long videoId) {
        boolean b = videoService.updateByVideoId(video, videoId);

        return new Result(true, StatusCode.OK, "更新成功");

    }


    /**
     * 删除
     * @param userId
     * @param videoId
     * @return
     */
    @RequestMapping(value = "/{userId}/{videoId}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable Long userId, @PathVariable Long videoId) {
        videoService.deleteById(userId, videoId);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @DeleteMapping("batchDelete")
    public Result batchDelete(@RequestBody List<Long> videoIds){
        videoService.batchDelete(videoIds);
        return new Result(StatusCode.OK);
    }

    /**
     * 上传视频/图片
     *
     * @param multipartFile
     * @param userId
     * @return
     */
    @RequestMapping(value = "/behave/uploadVideo", method = RequestMethod.POST)
    public Result uploadFile(@RequestAttribute MultipartFile multipartFile, @RequestParam("userId") Long userId) {

        VideoDTO videoDTO = videoService.uploadVideo(multipartFile, userId);

        if (videoDTO == null) {
            return new Result(false, StatusCode.ERROR, "上传失败", null);
        }
        return new Result(true, StatusCode.OK, "上传成功", videoDTO);
    }

    /**
     * 点赞
     *
     * @param videoId
     * @return
     */
    @PostMapping("/behave/thump")
    public Result thumbUpVideo(@RequestParam Long videoId, @RequestParam Long userId) {
        boolean success = videoService.thumpUpVideo(videoId, userId);
        if (success) {
            return new Result(true, StatusCode.OK, "点赞操作成功", true);
        }
        return new Result(false, StatusCode.ERROR, "点赞操作失败", false);
    }

    /**
     * 收藏
     *
     * @param videoId
     * @return
     */
    @PostMapping("/behave/collect")
    public Result collectVideo(@RequestParam Long videoId, @RequestParam Long userId) {
        boolean success = videoService.collectVideo(videoId, userId);
        if (success) {
            return new Result(true, StatusCode.OK, "收藏操作成功", true);
        }
        return new Result(false, StatusCode.ERROR, "收藏操作失败", false);
    }

    /**
     * 转发
     *
     * @param videoId
     * @return
     */
    @PostMapping("/behave/forward")
    public Result forwardVideo(@RequestParam Long videoId, @RequestParam Long userId) {
        boolean success = videoService.forwardVideo(videoId, userId);
        if (success) {
            return new Result(true, StatusCode.OK, "转发操作成功", true);
        }
        return new Result(false, StatusCode.ERROR, "转发操作失败", false);
    }

    /**
     * 获取视频的点赞评论收藏转发数量
     *
     * @param videoId
     * @param
     * @return
     */
    @GetMapping("/countVideoOprate")
    public Result countVideoOperate(@RequestParam Long videoId) {
        Map<String, Integer> countMap = videoService.countVideoOperate(videoId);
        return new Result(true, StatusCode.OK, "获取成功", countMap);
    }

    /**
     * 视频评论修改
     *
     * @param videoId
     * @param modify
     * @return
     */
    @PutMapping("/modifyVideoComment")
    public Result modifyVideoCommentByVideoId(@RequestParam String videoId, @RequestParam Integer modify) {
        videoService.modifyVideoCommentByVideoId(Long.valueOf(videoId), modify);
        return new Result(true, StatusCode.OK, "操作成功");
    }

    /**
     * 根据操作码和UserId获取相关视频
     *
     * @param userId
     * @param operate
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getVideoByUserIdAndOperate/{page}/{size}")
    public Result getVideoByUserIdAndOperate(@RequestParam Long userId, @RequestParam Integer operate, @PathVariable Integer page, @PathVariable Integer size) {
        List<Video> videoList = videoService.getVideoByUserIdAndOperate(userId, operate, page, size);
        return new Result(true, StatusCode.OK, "查询成功", videoList);
    }

    public Result countVideoByUserIdAndOperate(@RequestParam Long userId, @RequestParam Integer operate) {
        Integer count = videoService.countVideoByUserIdAndOperate(userId, operate);
        return new Result(true, StatusCode.OK, "查询成功", count);
    }


    @PostMapping("/backend/search/{page}/{size}")
    public Result backendSearchVideo(@RequestBody Map searchMap, @PathVariable("page") int page, @PathVariable("size") int size,@RequestParam(required = false) Long operateUserId) {
        Page<Video> search = videoService.findSearch(searchMap, page, size,operateUserId);
        return new Result(new PageResult<>(search.getTotalElements(), search.getContent()));
    }

    @GetMapping("/getUserVideoTotalThumbs")
    public Result getUserVideoTotalThumbs(@RequestParam Long userId){
        Integer count  = videoService.getUserVideoTotalThumbs(userId);
        return new Result(count);
    }
    @PostMapping("/findBySearch/{page}/{size}")
    public Result findBySearch(@RequestBody Map map, @PathVariable("page") int page, @PathVariable("size") int size){
        return new Result(videoService.findBySearch(map,page,size));
    }

    /**
     * 教师板块
     * @param page
     * @param size
     * @param operateUserId
     * @param type
     * @return
     */
    @GetMapping("/getTeacherVideo/{page}/{size}")
    public Result getTeacherVideo(@PathVariable int page, @PathVariable int size,@RequestParam(required = false) Long operateUserId,@RequestParam(required = false) Integer type) {
        Page<Video> pageResult = videoService.getTeacherVideo(page, size, operateUserId);
        return new Result(new PageResult<>(pageResult.getTotalElements(),pageResult.getContent()));
    }

    /**
     * 关注板块
     * @param page
     * @param size
     * @param operateUserId
     * @param type
     * @return
     */
    @GetMapping("/getFollowVideo/{page}/{size}")
    public Result getFollowVideo(@PathVariable int page, @PathVariable int size,@RequestParam(required = false) Long operateUserId,@RequestParam(required = false) Integer type) {
        return new Result(videoService.getFollowVideo(page, size, operateUserId));
    }

    /**
     * 好友板块
     * @param page
     * @param size
     * @param operateUserId
     * @param type
     * @return
     */
    @GetMapping("/getFriendVideo/{page}/{size}")
    public Result getFriendVideo(@PathVariable int page, @PathVariable int size,@RequestParam(required = false) Long operateUserId,@RequestParam(required = false) Integer type) {
        return new Result(videoService.getFriendVideo(page, size, operateUserId));
    }

    /**
     * 推荐板块
     * @param page
     * @param size
     * @param operateUserId
     * @param type
     * @return
     */
    @GetMapping("getRecommendVideo/{page}/{size}")
    public Result getRecommendVideo(@PathVariable int page,@PathVariable int size,@RequestParam(required = false) Long operateUserId,@RequestParam(required = false) Integer type) {
        return new Result(videoService.getRecommendVideo(page,size,operateUserId));
    }
    @GetMapping("getVideoByUserIdAndOperate")
    public Result getVideoByUserIdAndOperateUserId(@RequestParam Long userId,@RequestParam Long videoId){
        return new Result(videoService.getVideoByUserIdAndOperateUserId(userId,videoId));
    }


}
